package com.neusoft.ysu.elm02.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.ysu.elm02.entity.CommonResult;
import com.neusoft.ysu.elm02.entity.Emp;
import com.neusoft.ysu.elm02.mapper.EmpMapper;
import com.neusoft.ysu.elm02.service.IEmpService;
import com.neusoft.ysu.elm02.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者：赵子鉴
 * @since 2023-08-21
 */
@Service
public class EmpServiceImpl implements IEmpService {
    @Autowired
    private EmpMapper empMapper;



    @Override
    public CommonResult findAll() {
// empMapper.findAllEmpsWithDept(); //

        List<Emp> emps = empMapper.findAllWithDept(); // empMapper.selectList(null);
        CommonResult<Emp> empCommonResult = new CommonResult(emps);
        return empCommonResult;
    }

    @Override
    public CommonResult insert(Emp emp) {
        int i=empMapper.insert(emp);
        return new CommonResult<>(emp.getDeptno());//insert返回主键列生成的值
    }

    @Override
    public CommonResult findJobByPre(String job) {
        job="*".equals(job)?"":job;
        List<String> list=empMapper.findJobByPre(job);

        return new CommonResult<>(list);
    }

    @Override
    public CommonResult checkLogin(String loginname, String password) throws UnsupportedEncodingException {
        Emp emp=empMapper.login(loginname,password);
        if(emp!=null && loginname.equals(emp.getEname())){
            HashMap<String,String> map=new HashMap<>();
            map.put("loginname",loginname);
            map.put("job",emp.getJob());
            String token= JWTUtil.buildToken(map);
            emp.setToken(token);
            return  new CommonResult<>(emp);
        }

        return null;
    }

}
